package com.example.administrator.chinafood;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.chinafood.SQLite.MyDBOpenHelper;

public class Register extends AppCompatActivity {

    private Button btn_register;
    private Button btn_to_login;
    private EditText ed_username;
    private EditText ed_password;
    private EditText ed_repassword;

    private String username;
    private String password;
    private String repassword;

    private MyDBOpenHelper myDBOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        myDBOpenHelper = new MyDBOpenHelper(Register.this,"ChinaFood.db");
        init();

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = ed_username.getText().toString().trim();
                password = ed_password.getText().toString().trim();
                repassword = ed_repassword.getText().toString().trim();
                registerUser();
            }
        });

        btn_to_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(Register.this,Login.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void init(){
        btn_register = (Button)findViewById(R.id.register_register);
        btn_to_login = (Button)findViewById(R.id.register_to_login);
        ed_username = (EditText)findViewById(R.id.register_username);
        ed_password = (EditText)findViewById(R.id.register_psd);
        ed_repassword = (EditText)findViewById(R.id.register_repsd);
    }

    private void registerUser(){
        SQLiteDatabase db = myDBOpenHelper.getWritableDatabase();
        if(username.equals("") || password.equals("")){
            Toast.makeText(Register.this,"用户名或者密码不能为空",Toast.LENGTH_SHORT).show();
        } else {
            if (!password.equals(repassword)) {
                Toast.makeText(Register.this,"两次密码不一致",Toast.LENGTH_SHORT).show();
            }else {
                Cursor cursor = db.rawQuery("select username from user where username = ?",new String[]{username});
                if (cursor.getCount() == 1){
                    Toast.makeText(Register.this,"用户名已存在",Toast.LENGTH_SHORT).show();
                    cursor.close();
                }else {
                    db.execSQL("insert into user(username,password) values(?,?)", new String[]{username, password});
                    Toast.makeText(Register.this,"注册完成",Toast.LENGTH_SHORT).show();
                }
            }
        }
        db.close();
    }
}
